package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.CategoryDTO;
import com.lastproject.ejb.entities.Category;

/**
 * Session Bean implementation class CategoryConversion
 */
@Stateless
@LocalBean
public class CategoryConversion {
	@PersistenceContext
	EntityManager em;

	public CategoryDTO fromEntity(Category u) {

		CategoryDTO category = new CategoryDTO();
		category.setId(u.getId());
		category.setName(u.getName());

		return category;
	}

	public Category fromDTO(CategoryDTO u) {

		Category category = em.find(Category.class, u.getId());

		if (category == null) {
			category = new Category();
		}

		category.setId(u.getId());
		category.setName(u.getName());
		
		return category;
	}

}
